import {GameEngine} from "./gameEngine"

export class Enemys {
  id: number = 0;
  el:HTMLDivElement;
  timer:number = 0;
  hp:number = 2;
  constructor(){
    this.id = parseInt(Math.random()*100000+""); 
    this.el = document.createElement("div");
    this.el.className = "enemy-small";
    GameEngine.instance().el.appendChild(this.el);
    GameEngine.instance().enemys[this.id] = this;
    let eX = GameEngine.instance().el.offsetWidth * Math.random()-this.el.offsetWidth;
    if (eX <= 0) eX = 0;
    this.el.style.left = parseInt(eX + "") + "px";
    this.el.style.top = "0px";

    this.move();
  }

  move() {
    let step = 5;
     this.timer = setInterval(() => {
      this.el.style.top = this.el.offsetTop + step + "px";
      if(this.el.offsetTop > GameEngine.instance().el.offsetHeight){
        clearInterval(this.timer);
      }
     },100)
  }

  boom() {
    let num = 0;
    let timer = setInterval(() => {
      num++;
      this.el.style.backgroundImage = `url(/src/assets/images/plane1_die${num}.png)`;
      if(num == 3) {
        clearInterval(timer);
        GameEngine.instance().enemys.splice(this.id,1);
        this.el.remove();
      }
    },100);
  }


  hurt() {
    this.hp--;
    if (this.hp <= 0) {
      this.boom();
    }
  }
}